﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GDClippingImageView.h"
#include "../GdDefine.h"
#include "../Util/GDUtil.h"

GDClippingImageView* GDClippingImageView::createWithFrame(const CCRect& rect, CAImage *image, std::string imageUrl)
{
	GDClippingImageView * pRet = new GDClippingImageView();
	if (pRet && pRet->initWithFrame(rect))
	{
		pRet->initView(image, imageUrl);
		pRet->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRet);
	}
	return pRet;
}

GDClippingImageView::GDClippingImageView(void)
:scrollView(NULL)
, imageView(NULL)
, headNode(NULL)
, btmNode(NULL)
, titleNabel(NULL)
, cancelBtn(NULL)
, okBtn(NULL)
, m_clvImage(NULL)
, m_clippingImageDelegate(NULL)
, m_clv(NULL)
, zhezhaoHeight(0)
, btmZhezhaoHeight(0)
{

}


GDClippingImageView::~GDClippingImageView(void)
{
}

CADrawView* GDClippingImageView::getStencil(const CCSize& size, int index)
{
	if (index == 0)
	{
		CCPoint ver[4];
		ver[0] = CCPoint(0, 0);
		ver[1] = CCPoint(0, size.height);
		ver[2] = CCPoint(size.width, size.height);
		ver[3] = CCPoint(size.width, 0);
		CADrawView* stencil = CADrawView::create();
		stencil->drawPolygon(ver, 4, ccc4f(255, 0, 0, 0), 2, ccc4f(255, 0, 0, 0));
		stencil->setFrameOrigin(CCPoint(0, size.height));
		return stencil;
	}
	else if (index == 1)
	{
		CCPoint cir[720];
		for (int i = 0; i<720; i++)
		{
			float x = cosf(i * M_PI / 180.f) * size.width / 2;
			float y = sinf(i * M_PI / 180.f) * size.width / 2;
			cir[i] = CCPoint(x, y);
		}
		CADrawView* stencil = CADrawView::create();
		stencil->drawPolygon(cir, 720, ccc4f(1, 1, 1, 0.5), 0, ccc4f(1, 1, 1, 0));
		stencil->setCenterOrigin(CCPoint(size.width / 2, size.height / 2));
		return stencil;
	}
	return NULL;
}

void GDClippingImageView::initView(CAImage *image, std::string imageUrl)
{
	winSize = this->getBounds().size;

	float top_gap = 0;
	
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
	top_gap = _ds(30.0);
	#endif

	
	zhezhaoHeight = _ds(85);

	/*
	scrollView = CAScrollView::createWithFrame(CCRect(0, headNode->getBounds().size.height, winSize.width, winSize.height - headNode->getBounds().size.height - btmNode->getBounds().size.height));
	scrollView->setBounceHorizontal(false);
	scrollView->setBounceVertical(false);

	scrollView->setShowsHorizontalScrollIndicator(false);
	scrollView->setShowsVerticalScrollIndicator(false);

	this->addSubview(scrollView);

	imageView = GDImageView::createWithFrame(CCRect(0, 0, winSize.width, winSize.width));
	scrollView->addSubview(imageView);
	imageView->setDelegate(this);

	
	btmZhezhaoHeight = scrollView->getBounds().size.height - winSize.width - zhezhaoHeight;
	if (btmZhezhaoHeight < 0){
		btmZhezhaoHeight = 0;
	}

	CAView *topZhezhezhaoView = CAView::createWithFrame(CCRect(0, scrollView->getFrameOrigin().y, winSize.width, zhezhaoHeight), ccc4(0, 0, 0, 200));
	this->insertSubview(topZhezhezhaoView,3);

	CAView *buttomZhezhezhaoView = CAView::createWithFrame(CCRect(0, scrollView->getFrameOrigin().y + scrollView->getBounds().size.height - btmZhezhaoHeight, winSize.width, btmZhezhaoHeight), ccc4(0, 0, 0, 200));
	this->insertSubview(buttomZhezhezhaoView, 3);
	*/

	int index = 0;
	CCRect scrollRect;
	scrollRect.origin.x = 0;
	scrollRect.origin.y = _ds(68) + top_gap + zhezhaoHeight;
	scrollRect.size.width = winSize.width;
	scrollRect.size.height = scrollRect.size.width;

	m_clvImage = CAClippingView::create();
	m_clvImage->setStencil(getStencil(scrollRect.size, index));
	m_clvImage->setFrame(scrollRect);
	m_clvImage->setInverted(false);
	m_clvImage->setClippingEnabled(false);
	this->addSubview(m_clvImage);

	scrollView = CAScrollView::createWithFrame(m_clvImage->getBounds());
	
	//scrollView->setBackGroundColor(CAColor_green);
	scrollView->setShowsScrollIndicators(false);
	scrollView->setBounces(false);
	//scrollView->setScrollViewDelegate(this);
	scrollView->setDisplayRange(true);
	m_clvImage->addSubview(scrollView);

	CCRect rect;
	rect.origin = CCPointZero;
	rect.size = scrollView->getViewSize();
	imageView = GDImageView::createWithFrame(rect);
	//imageView->setColor(CAColor_red);
	scrollView->addSubview(imageView);


	m_clv = CAClippingView::create();
	m_clv->setStencil(getStencil(scrollRect.size, index));
	m_clv->setFrame(scrollRect);
	m_clv->setInverted(true);
	m_clv->setTouchEnabled(false);
	this->addSubview(m_clv);

	CCRect ivRect;
	ivRect.size = winSize;
	ivRect.origin = ccpMult(scrollRect.origin, -1);
	CAView* iv = CAView::createWithColor(ccc4(0, 0, 0, 128));
	iv->setFrame(ivRect);
	m_clv->addSubview(iv);
	
	headNode = CAView::createWithFrame(CCRect(0, 0, winSize.width, _ds(68) + top_gap), GD_COLOR_f0f0f0); // GD_COLOR_f0f0f0
	this->addSubview(headNode);
	titleNabel = GDUtil::createLabelWithFrame(CCRect(0, headNode->getBounds().size.height - _ds(68), winSize.width, _ds(68)), "截图", GD_COLOR_646464, _ds(32), "", CATextAlignmentCenter, CAVerticalTextAlignmentCenter);
	headNode->addSubview(titleNabel);

	btmNode = CAView::createWithFrame(CCRect(0, winSize.height - _ds(88), winSize.width, _ds(88)));
	this->addSubview(btmNode);

	// 取消按
	cancelBtn = CAButton::createWithFrame(CCRect(_ds(20), _ds(24), _ds(92), _ds(40)), CAButtonTypeCustom);
	btmNode->addSubview(cancelBtn);
	cancelBtn->addSubview(GDUtil::createLabelWithFrame(CCRect(_ds(10), 0, _ds(70), _ds(40)), "取消", GD_COLOR_969696, _ds(24), "", CATextAlignmentLeft, CAVerticalTextAlignmentCenter));
	cancelBtn->addTarget(this, CAControl_selector(GDClippingImageView::onCancelBtnClick), CAControlEventTouchUpInSide);

	okBtn = CAButton::createWithFrame(CCRect(winSize.width - _ds(112), _ds(24), _ds(92), _ds(40)), CAButtonTypeCustom);
	btmNode->addSubview(okBtn);
	okBtn->setBackGroundViewForState(CAControlStateAll, CAImageView::createWithImage(CAImage::create(GD_IMAGE_PATH + "btn_bg_3.png")));
	okBtn->addSubview(GDUtil::createLabelWithFrame(okBtn->getBounds(), "下一步", CAColor_white, _ds(24), "", CATextAlignmentCenter, CAVerticalTextAlignmentCenter));
	okBtn->addTarget(this, CAControl_selector(GDClippingImageView::onOkBtnClick), CAControlEventTouchUpInSide);


	if (image != NULL){
		imageView->setDefaultImage(image, true);
		freshScrollView();
	}
	else{
		imageView->setUrl(imageUrl);
	}
}




void GDClippingImageView::imageViewRequestFinished(CommonUrlImageView* imageView)
{
	freshScrollView();
}

void GDClippingImageView::freshScrollView()
{
	CCSize imageSize = imageView->getCommonUrlImageView()->getImage()->getContentSize();
	float minSize = MIN(imageSize.width, imageSize.height);

	if (minSize < winSize.width){
		imageView->setFrame(CCRect(imageView->getFrameOrigin().x, imageView->getFrameOrigin().y, imageSize.width*winSize.width / minSize, imageSize.height*winSize.width / minSize));
	}
	else{
		imageView->setFrame(CCRect(imageView->getFrameOrigin().x, imageView->getFrameOrigin().y, imageSize.width, imageSize.height));
	}

	imageSize = imageView->getBounds().size;
	minSize = MIN(imageSize.width, imageSize.height);

	float temp_mini = MAX(0.4, MIN(0.9, winSize.width / minSize));

	scrollView->setViewSize(imageSize);
	scrollView->setContentOffset(CCPoint(MAX(0, (imageSize.width - scrollView->getBounds().size.width) / 2), MAX(0, (imageSize.height - scrollView->getBounds().size.height) / 2)), false);
	scrollView->setMinimumZoomScale(temp_mini);
	scrollView->setMaximumZoomScale(3);
}

void GDClippingImageView::onCancelBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	if (m_clippingImageDelegate){
		m_clippingImageDelegate->onClippingCancel();
	}
}

void GDClippingImageView::onOkBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	CAImage *image = clipImage();
	if (image != NULL){
		if (m_clippingImageDelegate){
			m_clippingImageDelegate->onClippingFinished(image);
		}
	}
}

CAImage* GDClippingImageView::clipImage()
{
	m_clvImage->setClippingEnabled(true);
	CARenderImage* rm = CARenderImage::create(winSize.width, winSize.width);
	rm->printscreenWithView(m_clvImage);
	m_clvImage->setClippingEnabled(false);

	CAImage *image = rm->getImageView()->getImage();
	
	return image;
}